package com.yami.shop.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Ecosphere;
import com.yami.shop.bean.model.EcosphereClassification;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.EcosphereClassificationService;
import com.yami.shop.service.EcosphereService;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/ecosphere")
public class EcosphereController {

    @Autowired
    private EcosphereService ecosphereService;
    @Autowired
    private EcosphereClassificationService ecosphereClassificationService;
    @Autowired
    private UserService userService;
    @GetMapping("/getAllEcosphereClassification")
    @Operation(summary =  "查询", description = "查询分类")
    public ServerResponseEntity<List<EcosphereClassification>> getNoticePage() {
        List<EcosphereClassification> noticePage = ecosphereClassificationService.list(new LambdaQueryWrapper<EcosphereClassification>()
                .eq(EcosphereClassification::getStatus, 0).orderByAsc(EcosphereClassification::getSort));
        return ServerResponseEntity.success(noticePage);
    }


    @GetMapping("/pageEcosphere")
    @Operation(summary =  "分页查询", description = "分页查询")
    public ServerResponseEntity<IPage<Ecosphere>> getNoticePage(PageParam<Ecosphere> page, Ecosphere ecosphere,String userId) {
        Boolean ageCheck = true;
        if(StrUtil.isNotBlank(userId)){
            User userByUserId = userService.getUserByUserId(userId);
            ageCheck = ObjectUtil.isEmpty(userByUserId.getIdNumber())?true:userService.isBigMan(userByUserId.getIdNumber());
        }
        IPage<Ecosphere> noticePage = ecosphereService.page(page, new LambdaQueryWrapper<Ecosphere>()
                .eq(Ecosphere::getStatus, 0)
                .ne(!ageCheck, Ecosphere::getAgeCheck, 1)
                .eq(StrUtil.isNotEmpty(ecosphere.getEcosphereCode()),Ecosphere::getEcosphereCode, ecosphere.getEcosphereCode())
                .orderByDesc(Ecosphere::getCreateTime));

        return ServerResponseEntity.success(noticePage);
    }
}
